<template>
  <div>
    <el-dialog :title="obj.isAdd?'管理员添加':'管理员编辑'" :visible.sync="obj.isShow" width="30%">
      <!-- 中间表单 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择活动区域">
            <el-option label="请选择" :value="0"></el-option>
            <el-option
              v-for="(item, index) in RoleList"
              :key="index"
              :value="item.id"
              :label="item.rolename"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="form.password"
           show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd">确 定</el-button>
        <el-button type="primary" @click="edit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { mgeAdd,msgInfo,msgEdit} from "../../../request/api.js";
import {sucessAlert,warningAlert} from "../../../utils/alert.js"
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      form: {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      RoleList: "role/list",
    }),
  },
  methods: {
    ...mapActions({
      asynRolelist: "role/asynclist",
      msgasynclist:"manage/asyncList",
      msgasyncTotal:"manage/asyncTotal",
    }),
    //清空表单
    empty() {
      this.form = {
        roleid: 0,
        username: "",
        password: "",
        status: 1,
      };
    },
    //取消
    cancel() {
      this.empty();
      this.obj.isShow = false;
    },
    //添加
    add() {
      mgeAdd(this.form).then(res => {
         if(res.data.code == 200){
          // 成功的提示窗
          sucessAlert(res.data.msg)
          // 关闭遮罩层并清空form数据
          this.cancel()
          // 刷新列表数据
               this.msgasynclist()
          //刷新总条数
          this.msgasyncTotal()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      });
    },
    //获取一条信息
    getOne(uid){
      msgInfo({uid:uid}).then(res=>{
        this.form = res.data.list
        //密码置空
          this.form.password = ""
      })
    },
     // 点击确定编辑
    edit(){
      msgEdit(this.form).then(res=>{
        if(res.data.code == 200){
          // 成功的提示窗
          sucessAlert(res.data.msg)
          // 关闭遮罩层并清空form数据
          this.cancel()
          // 刷新列表数据
          this.msgasynclist()
        }else{
          // 失败的提示窗
          warningAlert(res.data.msg)
        }
      })
    },

  },
  mounted() {
    this.asynRolelist();
    this.msgasynclist()
  },
};
</script>
<style scoped>
</style>